# Load All R Functions
# Alex F. Gazmararian  
# agazmararian@gmail.com
# 
# This script loads all functions from the R/ directory modules
# Source this file to make all project functions available

#' Load all R functions from project modules
#' @param modules Character vector of module names to load (default: all)
#' @param verbose Whether to print loading messages (default: TRUE)
load_project_functions <- function(modules = c("utils", "statements", "visibility", "surveys", "covariates", "publicfinance", "annotation"), 
                                  verbose = TRUE) {
  
  if (verbose) message("Loading project functions...")
  
  # Load utils first (others may depend on it)
  if ("utils" %in% modules) {
    if (verbose) message("  Loading utils...")
    utils_files <- list.files(here::here("R", "utils"), pattern = "\\.R$", full.names = TRUE)
    for (file in utils_files) {
      source(file, local = FALSE)
    }
  }
  
  # Load core modeling functions from main R directory
  if (verbose) message("  Loading core modeling functions...")
  core_files <- list.files(here::here("R"), pattern = "\\.R$", full.names = TRUE)
  # Exclude load_functions.R and config.R to avoid circular dependencies
  core_files <- core_files[!basename(core_files) %in% c("load_functions.R", "config.R")]
  for (file in core_files) {
    source(file, local = FALSE)
  }
  
  # Load other modules
  remaining_modules <- setdiff(modules, "utils")
  
  for (module in remaining_modules) {
    module_path <- here::here("R", module)
    if (dir.exists(module_path)) {
      if (verbose) message(sprintf("  Loading %s...", module))
      
      # Load R files recursively from module and subdirectories
      r_files <- list.files(module_path, pattern = "\\.R$", full.names = TRUE, recursive = TRUE)
      
      # Skip files that are scripts rather than function definitions
      # For covariates module, only load processing_functions.R (the actual functions)
      # For annotation module, skip all scripts as they are data processing scripts
      if (module == "covariates") {
        r_files <- r_files[basename(r_files) == "processing_functions.R"]
      } else if (module == "annotation") {
        r_files <- c()  # Skip all annotation scripts - they are data processing scripts
      } else {
        skip_files <- c("clean_qualtrics_covariates.R", "run_all_covariates.R")
        r_files <- r_files[!basename(r_files) %in% skip_files]
      }
      
      for (file in r_files) {
        source(file, local = FALSE)
      }
    } else {
      if (verbose) message(sprintf("  Module %s not found, skipping", module))
    }
  }
  
  if (verbose) message("All functions loaded successfully!")
  invisible(NULL)
}

# Automatically load all functions when this file is sourced
if (!exists(".functions_loaded") || !.functions_loaded) {
  load_project_functions()
  .functions_loaded <- TRUE
}

# Standardized political party colors
party_colors <- list(
  Democrat = "#3B6BA5",
  Republican = "#B22222", 
  Independent = "#6AA84F",
  Neither = "#6AA84F"  # Using same color as Independent for "Neither" category
)

# Color scale functions for consistent usage
scale_color_party <- function() {
  ggplot2::scale_color_manual(values = c(
    "Democrat" = party_colors$Democrat,
    "Republican" = party_colors$Republican,
    "Independent" = party_colors$Independent,
    "Neither" = party_colors$Neither
  ))
}

scale_fill_party <- function() {
  ggplot2::scale_fill_manual(values = c(
    "Democrat" = party_colors$Democrat,
    "Republican" = party_colors$Republican,
    "Independent" = party_colors$Independent,
    "Neither" = party_colors$Neither
  ))
}
